// pages/addyy/addyy.js
var common = require('../../js/common.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    yysxlist: [],
    // type: 1,
    yishenglist: [],
    // ysindex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '预约详情'
    })
    var that = this;
    console.log('id2=' + options.id)
    //获取权限列表
    common.request(app.globalData.localUrl, 'authuser', {
      yiyuan_id: wx.getStorageSync('yiyuan_id'),
      user_id: wx.getStorageSync('userInfo').id
    },
      function (res) {
        console.log('权限列表', res)
        // console.log(res.data.data.gsy_status)
        that.setData({
          quanxian: res.data,
          yjyz: wx.getStorageSync('userInfo').type
        })
      }, 'rtype= post');
    //获取积分
    common.request(app.globalData.localUrl, 'jiuzhenDetail', {
        id: options.id
      },
      function(res) {
        console.log('预约详情', res)
        if (res.data.code == 1) {
          that.setData({
            xqlist: res.data.data,
            is_status: res.data.data.is_status,
            yisheng_id: res.data.data.yisheng_id,
            date: res.data.data.yuyuetime,
            startime: res.data.data.startime,
            endtime: res.data.data.endtime,
            other_id: res.data.data.other_id,
            type: res.data.data.type,
            
            // is_status: res.data.data.is_status,
          })
        }
      }, 'rtype= post');
    common.request(app.globalData.localUrl, 'yishenglist', {
        id: wx.getStorageSync('yiyuan_id'),
        user_id: wx.getStorageSync('userInfo').id
      },
      function(res) {
        console.log('医生列表', res)
        if (res.data.code == 1) {
          var data = res.data.data;
          var data1 = {
            id: '0',
            user_id: 647,
            y_id: 14,
            user_phone: 13669798889,
            createtime: 1546786839,
            is_del: 0,
            name: '未选定医生',
          }
          data.unshift(data1);

          console.log('插入好的data', data)
          that.setData({
            yishenglist: data
          })
       

          for (var index in  data) {
            if ( data[index].id == that.data.yisheng_id){
              that.setData({
                ysindex: index
              })
            }
          }
        }
      }, 'rtype= post');

    var that = this;
    //获取积分
    common.request(app.globalData.localUrl, 'typelist', {},
      function (res) {
        console.log('预约事项列表', res)
        if (res.data.code == 1) {
          that.setData({
            yysxlist: res.data.data
          })

          for (var index in res.data.data) {
            if (res.data.data[index].id == that.data.other_id) {
              that.setData({
                yysxindex: index
              })
            }
          }
        }
      }, 'rtype= post');
  },
  //日期选择器：
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },

  //开始时间选择器：
  bindstartimeChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      startime: e.detail.value
    })
  },
  //预约医生
  bindyishengChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      ysindex: e.detail.value
    })
  },

  //结束时间选择器：
  bindendtimeChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endtime: e.detail.value
    })
  },

  //预约事项
  bindPickerChange2: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      yysxindex: e.detail.value
    })
  },
  //就诊时长
  bindTimeChange: function(e) {
    this.setData({
      timeindex: e.detail.value
    });
  },
  //就诊类型改变
  changtype: function(e) {
    var id = e.target.dataset.id;
    this.setData({
      type: id
    });
  },

  //就诊状态改变
  changstatus: function (e) {
    var id = e.target.dataset.id;
    this.setData({
      is_status: id
    });
  },

  //提交
  formSubmit: function(e) {
    var that = this;

    wx.showLoading({
      title: '提交中',
      mask: true
    })


    if (e.detail.value.huanname == '') {
      wx.showToast({
        title: '患者姓名不为空',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    // var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    // if (e.detail.value.phone) {
    //   if (e.detail.value.phone.length < 11) {
    //     wx.showToast({
    //       title: '手机号长度有误！',
    //       icon: 'none',
    //       duration: 1500
    //     });
    //     return false;
    //   } else if (!myreg.test(e.detail.value.phone)) {
    //     wx.showToast({
    //       title: '手机号有误！',
    //       icon: 'none',
    //       duration: 1500
    //     });
    //     return false;
    //   }
    // }

 
    var data = {
      id: that.data.xqlist.id,
      huanname: e.detail.value.huanname,
      time: that.data.date,
      startime: that.data.startime,
      endtime: that.data.endtime,
      yiyuan_id: wx.getStorageSync('yiyuan_id'),
    };
    //医生id
    if (that.data.ysindex !=0) {
      data.yisheng_id = that.data.yishenglist[that.data.ysindex].id;
    }else{
      data.yisheng_id = 0;
    }
    //事项
    if (that.data.yysxindex) {
      data.other_id = that.data.yysxlist[that.data.yysxindex].id;
    }
    //联系方式
    if (e.detail.value.phone) {
      data.phone = e.detail.value.phone;
    }
    //预约类型
    if (that.data.type) {
      data.type = that.data.type;
    }
    //就诊状态
    if (that.data.is_status) {
      data.is_status = that.data.is_status;
    }
    //备注
    if (e.detail.value.rename) {
      data.rename = e.detail.value.rename;
    }

    console.log('详情改data=',data)
    common.request(app.globalData.localUrl, 'jiuzhenedit', data,
      function(res) {
        console.log('修改状态', res)
        if (res.data.code == 1) {
          wx.showToast({
            title: '修改状态成功',
            icon: 'success',
            mask: true
          })

          setTimeout(function() {
            var pages = getCurrentPages(); // 当前页面
            var beforePage = pages[pages.length - 2]; // 前一个页面
            // console.log("beforePage");
            // console.log(beforePage);

              var e ={};
            e.time = that.data.date;
            wx.navigateBack({
              success: function() {
                beforePage.onLoad(e); // 执行前一个页面的onLoad方法
              }
            });
          }, 1500);

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1500
          })
        }
      }
    );
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // }
})